package com.asp.alumnisystem.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.asp.alumnisystem.utils.ListUtils;
import com.asp.alumnisystem.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * BaseFragment
 */
public abstract class BaseFragment extends Fragment {

    private View contentView;
    private boolean isCreateView;
    private boolean isInitView;
    private boolean isVisibleToUser;

    private Unbinder unbinder;
    private List<BasePresenter> presenterList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (contentView == null){
            createUserView(inflater, container, savedInstanceState);
        }
        return contentView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        isCreateView = true;
    }

    private View createUserView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        if (contentView == null){
            contentView = createView(inflater, container, savedInstanceState);
            if (useButterKnife()){
                unbinder = ButterKnife.bind(this, contentView);
            }
            isInitView = false;                            //创建完后将是否初始化界面标志置为false
            if (contentView != null && isVisibleToUser){   //如果这个界面可见，则初始化界面信息并且将标志位职位true
                initView();
                isInitView = true;
                viewVisible();       //通知子类界面可见
            }
        }
        return contentView;
    }

    @Override
    public void setUserVisibleHint(boolean isVisible) {
        super.setUserVisibleHint(isVisibleToUser);

        //如果可见而且已经创建了view
        if (isVisible && isCreateView){
            //如果view不为空而且还没有初始化界面信息（该情况为fragment开始不可见，后来滑动过去）
            if (contentView != null && !isInitView){
                initView();          //初始化界面信息
                isInitView = true;   //将初始化界面信息标志位置为true
            }
            viewVisible();          //通知子类界面可见
        }else{
            if (isVisibleToUser){ //判断该值可避免在第一次界面还没有初始化就调用隐藏的情况
                viewGone();        //如果界面变为不可见了，通知子类界面不可见
            }
        }
        isVisibleToUser = isVisible;
    }

    public void managerPresenter(BasePresenter presenter){
        if (presenter == null){
            return;
        }
        if (presenterList == null){
            presenterList = new ArrayList<>();
        }
        presenterList.add(presenter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (ListUtils.isNotNull(presenterList)){
            for (BasePresenter basePresenter : presenterList) {
                basePresenter.destroy();
            }
            presenterList.clear();
        }
        destroy();
        if (unbinder != null){
            unbinder.unbind();
        }
    }

    /**
     * 是否使用 Butterknife
     */
    protected boolean useButterKnife(){
        return true;
    }

    /**
     * 获取实现类的baseAdapter，用于在请求失败是显示界面不同状态
     */
    public BaseAdapter getBaseAdapter() {
        return null;
    }

    /**
     * 开始请求
     */
    public void loadStart(BasePresenter presenter) { }

    /**
     * 请求失败
     */
    public void loadFail(BasePresenter presenter, String error) {
        if (getBaseAdapter() != null){
            getBaseAdapter().dataChange();
        }
        ToastUtil.showShortToast(error);
    }

    public void loadDone(BasePresenter presenter) { }

    protected void viewVisible() { }

    protected void viewGone() { }

    protected abstract View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState);
    protected abstract void initView();
    protected abstract void destroy();

}
